//
//  NSURL+OSSImage.m
//  CollagePicture
//
//  Created by simon on 16/10/21.
//  Copyright © 2016年 simon. All rights reserved.
//

#import "NSURL+OSSImage.h"

@implementation NSURL (OSSImage)


+ (nullable NSString *)zx_ossImageStringWithResizeType:(OSSImageResizeType)resizeType relativeToImgPath:(id)baseURL{
    
    return  [self zx_ossImageURLWithResizeType:resizeType relativeToImgPath:baseURL].absoluteString;
}


+ (nullable NSURL *)zx_ossImageURLWithResizeType:(OSSImageResizeType)resizeType relativeToImgPath:(id)baseURL
{
    NSString *baseString = nil;
    if ([baseURL isKindOfClass:[NSURL class]])
    {
        baseString = [[baseURL absoluteString] copy];
    }
    else if ([baseURL isKindOfClass:[NSString class]])
    {
        baseString = [baseURL copy];
    }else{
        return nil;
    }
    NSString *processStr =[baseString stringByAppendingString:@"?x-oss-process=image/resize,"];
    NSString *absoluteString = nil;
    switch (resizeType)
    {
        case OSSImageResizeType_w50_hX:
            absoluteString = [processStr stringByAppendingString:@"w_50"];
            break;
        case OSSImageResizeType_w100_hX:
            absoluteString = [processStr stringByAppendingString:@"w_100"];
            break;
        case OSSImageResizeType_w200_hX:
            absoluteString = [processStr stringByAppendingString:@"w_200"];
            break;
        case OSSImageResizeType_w300_hX:
            absoluteString = [processStr stringByAppendingString:@"w_300"];
            break;
        case OSSImageResizeType_w400_hX:
            absoluteString = [processStr stringByAppendingString:@"w_400"];
            break;
        case OSSImageResizeType_w500_hX:
            absoluteString = [processStr stringByAppendingString:@"w_500"];
            break;
        case OSSImageResizeType_w600_hX:
            absoluteString = [processStr stringByAppendingString:@"w_600"];
            break;
        case OSSImageResizeType_w700_hX:
            absoluteString = [processStr stringByAppendingString:@"w_700"];
            break;
        case OSSImageResizeType_w800_hX:
            absoluteString = [processStr stringByAppendingString:@"w_800"];
            break;
        case OSSImageResizeType_wScreen_hX:
            absoluteString = [processStr stringByAppendingFormat:@"w_%@",@([UIScreen mainScreen].currentMode.size.width)];
            break;
        case OSSImageResizeType_wScreenHalf_hX:
            absoluteString = [processStr stringByAppendingFormat:@"w_%@",@([UIScreen mainScreen].currentMode.size.width/2)];
            break;
        default:
            break;
    }
    return [NSURL URLWithString:absoluteString];
}

+ (nullable NSURL *)zx_ossImageURLWithResizeWidth:(CGFloat)width relativeToImgPath:(id)baseURL{
    
    NSString *absoluteString = [self zx_ossImageStringWithResizeWidth:width relativeToImgPath:baseURL];
    return [NSURL URLWithString:absoluteString];
}


+ (nullable NSString *)zx_ossImageStringWithResizeWidth:(CGFloat)width relativeToImgPath:(id)baseURL{
    
    NSString *baseString = nil;
    if ([baseURL isKindOfClass:[NSURL class]])
    {
        baseString = [[baseURL absoluteString] copy];
    }
    else if ([baseURL isKindOfClass:[NSString class]])
    {
        baseString = [baseURL copy];
    }else{
        return nil;
    }
    CGFloat resizeWidth = ceilf(width);
    NSString *processStr =[baseString stringByAppendingString:@"?x-oss-process=image/resize,"];
    NSString *absoluteString = [processStr stringByAppendingFormat:@"w_%@",@(resizeWidth)];
    return absoluteString;
}
@end
